import { LoginType } from "../consts/login_type";
import { User } from "../entity/user.entity";
import orm_util from "../utils/orm_util";
import { PlayerDao } from "./player_dao";

export class UserDao{
    public static ins() : UserDao{
        if (UserDao._ins == null){
            UserDao._ins = new UserDao();
        }
        return UserDao._ins;
    }

    private static _ins = null;

    public async findOrCreate(loginType:LoginType,data){

        let user:User=null;
        let userRepository=await orm_util.getRepository(User);
        switch(loginType){
          case LoginType.Test:
          user=await userRepository.findOne({ uniqueKey: "2323wwwe" });
          if(!user){
            user=new User();
            user.nickName="测试用户1";
            user.uniqueKey="2323wwwe";
            user.man=true;
            user.country="cn";
            user.avatarUrl="weqwer";
           //  user.avatarUrl=decoded.avatarUrl;
            user.loginType=LoginType.Test;
            let player=PlayerDao.ins().createPlayer();
            user.player=player;
            await userRepository.save(user);
          }
          break;
          case LoginType.WechatMini:
          default:
          console.error("不支持的参数in UserService findOrCreate")
          break;
        }
        return user;
    }

    
    
}